package com.ndood.spider.taobaomix.processor;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.ndood.core.util.UrlStringUtil;
import com.ndood.spider.taobaomix.bean.TaobaoMixGoods;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

/**
 * 方案一
 */
@Component
public class TaobaoMixPageProcessor implements PageProcessor {
	Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public Site getSite() {
		Site site = Site.me().setCharset("UTF-8").setCycleRetryTimes(3).setSleepTime(3 * 1000)
				.addHeader("Connection", "keep-alive").addHeader("Cache-Control", "max-age=0")
				.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0");
		return site;
	}

	@Override
	public void process(Page page) {
		System.out.println(page.getJson());
		if(page.getUrl()==null){
			return;
		}
		// Step1: 处理天猫商品
		if(page.getUrl().toString().contains("tmall")){
			if (page.getHtml().xpath("//*[@id='J_DetailMeta']").get()!=null) {
				try {
					processTmallGoodsDetail(page);
				} catch (Exception e) {
				}
				return;
			}
		}
		
		// Step2: 处理详情页
		if (page.getHtml().xpath("//*[@id='J_Title']/h3").get() != null) {
			try {
				processDetailsPage(page);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void processTmallGoodsDetail(Page page) throws Exception {
		// 获取商品url
		String url = page.getUrl().get();
		TaobaoMixGoods goods = new TaobaoMixGoods();
		goods.setUrl(url);
		
		// TODO 获取视频
		if(page.getJson().toString().contains("<video")){
			goods.getVedioUrls().put("video0", "tmall视频");
		}

		// 获取商品id
		Map<String, String> paramsMap = UrlStringUtil.getParamsMap(url);
		String id = paramsMap.get("id");
		goods.setId(id);

		// 获取店铺名称
		String shopName = page.getHtml().xpath("//*[@id='shopExtra']/div[@class='slogo']/a/strong/text()").get();
		goods.setShopName(shopName);

		// 获取商品名称
		String name = page.getHtml()
				.xpath("//*[@id='J_DetailMeta']/div[@class='tm-clear']/div[@class='tb-property']/div/div[@class='tb-detail-hd']/h1/text()")
				.get();
		name = name.replace("/", "_");
		goods.setName(name);

		// 获取商品价格 
		String price = page.getHtml().xpath("//*[@id='J_PromoPrice']/dd/div/span/text()").get();
		if(price==null){
			price = page.getHtml().xpath("//*[@id='J_PromoPrice']/dd/span/text()").get();
		}
		if(price==null){ 
			price = page.getHtml().xpath("//*[@id='J_StrPriceModBox']/dd/span/text()").get();
		}
		if(price==null){ 
			price = page.getHtml().xpath("//*[@id='J_StrPriceModBox']/em[2]/text()").get();
		}
		goods.setPrice(price);

		// 获取商品分类
		String cat = paramsMap.get("cat");
		if(cat!=null){
			goods.setCat(URLDecoder.decode(cat, "UTF-8"));
		}
		String catPath = cat;
		if(catPath!=null){
			goods.setCatPath(URLDecoder.decode(catPath, "UTF-8"));
		}
		// 获取商品图片
		List<Selectable> goodsImgs = page.getHtml().xpath("//*[@id='J_UlThumb']/li/a").nodes();
		Map<String, String> imgUrls = new HashMap<String, String>();
		for (int i = 0; i < goodsImgs.size(); i++) {
			Selectable goodsImg = goodsImgs.get(i);
			String goodsImgUrl = goodsImg.xpath("//*/img/@src").get();
			goodsImgUrl = goodsImgUrl.replaceAll("_([0-9]+)x([a-z0-9]+).jpg", "");
			imgUrls.put(name + i, goodsImgUrl);
		}
		goods.setImgUrls(imgUrls);

		// 获取颜色
		List<Selectable> colorImgs = page.getHtml()
				.xpath("//*[@id='J_DetailMeta']/div[@class='tm-clear']/div[@class='tb-property']/div/div[@class='tb-key']/div/div/dl[@class='tm-img-prop']/dd/ul/li")
				.nodes();
		Map<String, String> colorUrls = new HashMap<String, String>();
		for (int i = 0; i < colorImgs.size(); i++) {
			Selectable colorImg = colorImgs.get(i);
			// 获取颜色连接
			String colorUrl = colorImg.xpath("//*/a/@style").get();
			colorUrl = colorUrl.replaceAll("background:url\\(", "")
					.replaceAll("\\)([\\s]{0,})center([\\s]{0,})no-repeat;([\\s]{0,})", "");
			colorUrl = colorUrl.replaceAll("_([0-9]+)x([a-z0-9]+).jpg", "");

			// 获取颜色描述字段
			String colorName = colorImg.xpath("//*/a/span/text()").get();
			colorUrls.put(colorName, colorUrl);
		}
		goods.setColorUrls(colorUrls);

		// 获取月销量和评价
		String salesCount = page.getHtml()
				.xpath("//*[@id='J_DetailMeta']/div[@class='tm-clear']/div[@class='tb-property']/div/ul/li[@class='tm-ind-sellCount']/div/span[2]/text()")
				.get();
		if(salesCount==null){
			salesCount = page.getHtml().xpath("//*[@id='J_WrtAmount']/span[@class='tb-wrt-guc']/em/text()").get();
		}
		String commentsCount = page.getHtml().xpath("//*[@id='J_ItemRates']/div/span[2]/text()").get();
		goods.setSalesCount(salesCount);
		goods.setCommentsCount(commentsCount);

		// 获取品牌
		String brand = page.getHtml().xpath("//*[@id='J_BrandAttr']/div/a/text()").get();
		goods.setBrand(brand);

		// 获取参数
		List<Selectable> params = page.getHtml().xpath("//*[@id='J_AttrUL']/li").nodes();
		for (int i = 0; i < params.size(); i++) {
			Selectable param = params.get(i);
			String line = param.xpath("//*/text()").get().replace("&nbsp;", " ");
			if(line.split(":").length< 2){
				String key = line.split("：")[0];
				String value = line.split("：")[1];
				goods.getParams().put(key, value);
			}else{
				String key = line.split(":")[0];
				String value = line.split(":")[1];
				goods.getParams().put(key, value);
			}
		}

		// 获取商品描述 
		List<Selectable> descImgs = page.getHtml().xpath("//*[@id='description']/div/p/img").nodes();
		List<Selectable> descAllImgs = new ArrayList<Selectable>();
		if(page.getHtml().xpath("//*[@id='description']/div/table/tbody/tr/td/div/img").get()!=null){
			descImgs = page.getHtml().xpath("//*[@id='description']/div/table/tbody/tr/td/div/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		if(page.getHtml().xpath("//*[@id='description']/div/img").get()!=null){
			descImgs = page.getHtml().xpath("//*[@id='description']/div/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		if(page.getHtml().xpath("//*[@id='description']/div/p/span/img").get()!=null){
			descImgs = page.getHtml().xpath("//*[@id='description']/div/p/span/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		if(page.getHtml().xpath("//*[@id='description']/div/p/strong/span/img").get()!=null){
			descImgs = page.getHtml().xpath("//*[@id='description']/div/p/strong/span/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		if(page.getHtml().xpath("//*[@id='description']/div/p/strong/span/img").get()!=null){
			descImgs = page.getHtml().xpath("//*[@id='description']/div/p/strong/span/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		Map<String, String> descUrls = new HashMap<String, String>();
		for (int i = 0; i < descAllImgs.size(); i++) {
			Selectable descImg = descAllImgs.get(i);
			String descUrl = descImg.xpath("//*/@src").get();
			descUrl = descUrl.replaceAll("_([0-9]+)x([a-z0-9]+).jpg", "").replaceAll("https:", "");
			descUrls.put("商品描述" + i, descUrl);
		}
		goods.setDescUrls(descUrls);

		// 存入內存
		page.putField("goods" + id, goods);
	}
	
	private void processDetailsPage(Page page) throws Exception {
		// 获取商品url
		String url = page.getUrl().get();
		System.out.println(url);
		TaobaoMixGoods goods = new TaobaoMixGoods();
		goods.setUrl(url);
		
		// 获取商品分类
		String cat = UrlStringUtil.getParamsMap(url).get("cat");
		goods.setCat(cat);
		goods.setCatPath(cat);
		
		// 店铺名称
		String shopName = page.getHtml().xpath("//*[@id='J_ShopInfo']/div[@class='tb-shop-info-wrap']/div[@class='tb-shop-info-hd']/div[@class='tb-shop-name']/dl/dd/strong/a/@title").get();
		if(StringUtils.isEmpty(shopName)){
			shopName = page.getHtml().xpath("//*[@id='header-content']/div[2]/div[1]/div[1]/a/text()").get();
		}
		goods.setShopName(shopName);

		// 获取商品ID
		String id = page.getHtml().xpath("//*[@id='J_FrmBid']/input[@name='item_id']/@value").get();
		goods.setId(id);
		
		// 获取商品名称
		String name = page.getHtml().xpath("//*[@id='J_Title']/h3/text()").get();
		goods.setName(name);
		
		// 获取商品价格
		String price = page.getHtml().xpath("//*[@id='J_PromoPriceNum']/text()").get();
		if(price==null){
			price = page.getHtml().xpath("//*[@id='J_StrPrice']/em[2]/text()").get();
		}
		goods.setPrice(price);
		
		// 获取商品图片
		Map<String, String> imgUrls = new HashMap<String, String>();
		List<Selectable> imageNodes = page.getHtml().xpath("//*[@id='J_UlThumb']/li").nodes();
		for (int i = 0; i < imageNodes.size(); i++) {
			Selectable imageNode = imageNodes.get(i);
			if(imageNode.xpath("//*[@id='J_VideoThumb']").get()!=null){
				String vedioUrl = page.getHtml().xpath("//*[@id='detail']/div[1]/div[1]/div/div[1]/div[1]/div/div[2]/div/video").get();
				goods.getVedioUrls().put("商品视频"+i,"待下载-"+vedioUrl);
			}
			String goodsImgUrl = imageNode.xpath("//*/div/a/img/@src").get();
			goodsImgUrl = goodsImgUrl.replaceAll("_([0-9]+)x([a-z0-9]+).jpg", "");
			imgUrls.put(name + i, goodsImgUrl);
		}
		goods.setImgUrls(imgUrls);
		
		// 获取商品颜色
		Map<String, String> colorUrls = new HashMap<String, String>();
		List<Selectable> colorNodes = page.getHtml().xpath("//*[@id='J_isku']/div/dl[@class='J_Prop_Color']/dd/ul/li").nodes();
		for(int i=0;i<colorNodes.size();i++){
			Selectable colorNode = colorNodes.get(i);
			String goodsColorUrl = colorNode.xpath("//*/a/@style").get();
			String goodsColorName = colorNode.xpath("//*/a/span/text()").get();
			goodsColorUrl = goodsColorUrl.replaceAll("background:url\\(", "").replaceAll("\\)([\\s]{0,})center([\\s]{0,})no-repeat;([\\s]{0,})", "");
			goodsColorUrl = goodsColorUrl.replaceAll("_([0-9]+)x([a-z0-9]+).jpg", "");
			colorUrls.put(goodsColorName, goodsColorUrl);
		}
		goods.setColorUrls(colorUrls);
		
		// 获取销量和评论数
		String commentsCount = page.getHtml().xpath("//*[@id='J_RateCounter']/text()").get();
		String salesCount = page.getHtml().xpath("//*[@id='J_SellCounter']/text()").get();
		goods.setCommentsCount(commentsCount);
		goods.setSalesCount(salesCount);
		
		// 获取商品参数
		List<Selectable> goodsDescNodes = page.getHtml().xpath("//*[@id='attributes']/ul/li").nodes();
		for (int i=0;i<goodsDescNodes.size();i++) {
			Selectable goodsDescNode = goodsDescNodes.get(i);
			String line = goodsDescNode.xpath("//*/text()").get();
			String key = "";
			String value = "";
			if(line.split(":").length< 2){
				key = line.split("：")[0];
				value = line.split("：")[1];
			}else{
				key = line.split(":")[0];
				value = line.split(":")[1];
			}
			if(key.contains("品牌")){
				goods.setBrand(value);
			}else{
				goods.getParams().put(key, value);
			}
		}
		
		// 获取商品描述
		List<Selectable> descImgs = page.getHtml().xpath("//*[@id='J_DivItemDesc']/div/p/img").nodes();
		List<Selectable> descAllImgs = new ArrayList<Selectable>();
		if(page.getHtml().xpath("//*[@id='J_DivItemDesc']/p/img")!=null){
			descImgs = page.getHtml().xpath("//*[@id='J_DivItemDesc']/p/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		if(page.getHtml().xpath("//*[@id='J_DivItemDesc']/div/div/img").get()!=null){
			descImgs = page.getHtml().xpath("//*[@id='J_DivItemDesc']/div/div/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		if(page.getHtml().xpath("//*[@id='J_DivItemDesc']/div/img").get()!=null){
			descImgs = page.getHtml().xpath("//*[@id='J_DivItemDesc']/div/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		if(page.getHtml().xpath("//*[@id='J_DivItemDesc']/p/span/strong/span/span/img").get()!=null){
			descImgs = page.getHtml().xpath("//*[@id='J_DivItemDesc']/p/span/strong/span/span/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		if(page.getHtml().xpath("//*[@id='J_DivItemDesc']/p/span/strong/span/img").get()!=null){
			descImgs = page.getHtml().xpath("//*[@id='J_DivItemDesc']/p/span/strong/span/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		if(page.getHtml().xpath("//*[@id='J_DivItemDesc']/p/span/span/strong/span/img").get()!=null){
			descImgs = page.getHtml().xpath("//*[@id='J_DivItemDesc']/p/span/span/strong/span/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		if(page.getHtml().xpath("//*[@id='J_DivItemDesc']/p/strong/span/img").get()!=null){
			descImgs = page.getHtml().xpath("//*[@id='J_DivItemDesc']/p/strong/span/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		if(page.getHtml().xpath("//*[@id='J_DivItemDesc']/div/div/div/div/p/img").get()!=null){
			descImgs = page.getHtml().xpath("//*[@id='J_DivItemDesc']/div/div/div/div/p/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		if(page.getHtml().xpath("//*[@id='J_DivItemDesc']/p/strong/span/img").get()!=null){
			descImgs = page.getHtml().xpath("//*[@id='J_DivItemDesc']/p/strong/span/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		if(page.getHtml().xpath("//*[@id='J_DivItemDesc']/div/p/span/img").get()!=null){
			descImgs = page.getHtml().xpath("//*[@id='J_DivItemDesc']/div/p/span/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		if(page.getHtml().xpath("//*[@id='J_DivItemDesc']/img").get()!=null){
			descImgs = page.getHtml().xpath("//*[@id='J_DivItemDesc']/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		if(page.getHtml().xpath("//*[@id='J_DivItemDesc']/p/span/img").get()!=null){
			descImgs = page.getHtml().xpath("//*[@id='J_DivItemDesc']/p/span/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		if(page.getHtml().xpath("//*[@id='J_DivItemDesc']/table/tbody/tr/td/div/p/img").get()!=null){
			descImgs = page.getHtml().xpath("//*[@id='J_DivItemDesc']/table/tbody/tr/td/div/p/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		if(page.getHtml().xpath("//*[@id='J_DivItemDesc']/div/div/p/img").get()!=null){
			descImgs = page.getHtml().xpath("//*[@id='J_DivItemDesc']/div/div/p/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		if(page.getHtml().xpath("//*[@id='J_DivItemDesc']/table/tbody/tr/td/div/div/img").get()!=null){
			descImgs = page.getHtml().xpath("//*[@id='J_DivItemDesc']/table/tbody/tr/td/div/div/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		if(page.getHtml().xpath("//*[@id='J_DivItemDesc']/p/span/span/strong/font/img").get()!=null){
			descImgs = page.getHtml().xpath("//*[@id='J_DivItemDesc']/p/span/span/strong/font/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		if(page.getHtml().xpath("//*[@id='J_DivItemDesc']/p/strong/img").get()!=null){
			descImgs = page.getHtml().xpath("//*[@id='J_DivItemDesc']/p/strong/img").nodes();
			descAllImgs.addAll(descImgs);
		}
		Map<String, String> descUrls = new HashMap<String, String>();
		for (int i = 0; i < descAllImgs.size(); i++) {
			Selectable descImg = descAllImgs.get(i);
			String descUrl = descImg.xpath("//*/@src").get();
			descUrl = descUrl.replaceAll("_([0-9]+)x([a-z0-9]+).jpg", "").replaceAll("https:", "");
			descUrls.put("商品描述" + i, descUrl);
		}
		goods.setDescUrls(descUrls);

		// 存入內存
		page.putField("goods" + id, goods);
	}

}